package com.itheima.reggie.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.junit.Test;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;

/**
 * 短信发送工具类
 */
public class SMSUtils {

	/**
	 * 发送短信
	 * @param signName 签名
	 * @param templateCode 模板
	 * @param phoneNumbers 手机号
	 * @param param 参数
	 */
	public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "", "");
		IAcsClient client = new DefaultAcsClient(profile);

		SendSmsRequest request = new SendSmsRequest();
		request.setSysRegionId("cn-hangzhou");
		request.setPhoneNumbers(phoneNumbers);
		request.setSignName(signName);
		request.setTemplateCode(templateCode);
		request.setTemplateParam("{\"code\":\""+param+"\"}");
		try {
			SendSmsResponse response = client.getAcsResponse(request);
			System.out.println("短信发送成功");
		}catch (ClientException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送验证码到qq邮箱
	 * @throws EmailException
	 */

	public static String myEmail() throws EmailException {
		HtmlEmail email=new HtmlEmail();//创建一个HtmlEmail实例对象
		email.setHostName("smtp.qq.com");//邮箱的SMTP服务器，一般123邮箱的是smtp.123.com,qq邮箱为smtp.qq.com
		email.setCharset("utf-8");//设置发送的字符类型
		email.addTo("2844827619@qq.com");//设置收件人
		email.setFrom("1874487950@qq.com","yol");//发送人的邮箱为自己的，用户名可以随便填
		email.setAuthentication("1874487950@qq.com","tnpumymmgyhedbff");//设置发送人到的邮箱和用户名和授权码(授权码是自己设置的)
		email.setSubject("瑞吉外卖验证码");//设置发送主题
		String code = ValidateCodeUtils.generateValidateCode(4).toString();
		email.setMsg(code);//设置发送内容
		email.send();//进行发送
		return code;
	}




}
